package com.cw.jeeyt.service.converter.facade;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.cw.jeeyt.common.enums.EnumYn;
import com.cw.jeeyt.service.bo.facade.FacadeParamBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.model.facade.FacadeParamDo;
import com.cw.jeeyt.api.dto.facadeExcel.ApiParam;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.GsonUtil;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

/**
 * 接口参数转换类
 *
 * @author chenw
 * @date 2019-11-02 14:16:40
 */
public class FacadeParamConverter {


    /**
     * do转bo
     *
     * @param model
     * @return
     */
    public static FacadeParamBo do2Bo(FacadeParamDo model) {
        if (model == null) {
            return null;
        }
        return FacadeParamBo.builder()
            .id(model.getId())
            .facadeInfoId(model.getFacadeInfoId())
            .paramCode(model.getParamCode())
            .paramName(model.getParamName())
            .paramType(model.getParamType())
            .publicParam(model.getPublicParam())
            .paramComment(model.getParamComment())
            .fields(model.getFields())
            .createBy(model.getCreateBy())
            .createDate(model.getCreateDate())
            .updateBy(model.getUpdateBy())
            .updateDate(model.getUpdateDate())
            .build();
    }

    /**
     * bo转do
     *
     * @param bo
     * @return
     */
    public static FacadeParamDo bo2Do(FacadeParamBo bo) {
        if (bo == null) {
            return null;
        }
        FacadeParamDo model = new FacadeParamDo();
        model.setId(bo.getId());
        model.setFacadeInfoId(bo.getFacadeInfoId());
        model.setParamCode(bo.getParamCode());
        model.setParamName(bo.getParamName());
        model.setParamType(bo.getParamType());
        model.setPublicParam(bo.getPublicParam());
        model.setParamComment(bo.getParamComment());
        model.setFields(bo.getFields());
        model.setCreateBy(bo.getCreateBy());
        model.setCreateDate(bo.getCreateDate());
        model.setUpdateBy(bo.getUpdateBy());
        model.setUpdateDate(bo.getUpdateDate());
        return model;
    }

    /**
     * doList转boList
     *
     * @param doList
     * @return
     */
    public static List<FacadeParamBo> doList2BoList(List<FacadeParamDo> doList) {
        if (CollectionUtil.isEmpty(doList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(doList.iterator(),FacadeParamConverter::do2Bo));
    }

    /**
     * boList转doList
     *
     * @param boList
     * @return
     */
    public static List<FacadeParamDo> boList2DoList(List<FacadeParamBo> boList) {
        if (CollectionUtil.isEmpty(boList)) {
            return null;
        }
        return Lists.newArrayList(Iterators.transform(boList.iterator(),FacadeParamConverter::bo2Do));
    }

    /**
     * apiParam转do
     *
     * @param apiParam
     * @return
     */
    public static FacadeParamDo apiParam2Do(ApiParam apiParam) {
        if (apiParam == null) {
            return null;
        }
        FacadeParamDo paramDo = new FacadeParamDo();
        paramDo.setParamCode(apiParam.getParamCode());
        paramDo.setParamName(StringUtils.isBlank(apiParam.getParamName()) ? "未知" : apiParam.getParamName());
        paramDo.setParamType(StringUtils.isBlank(apiParam.getParamType()) ? "未知" : apiParam.getParamType());
        paramDo.setParamComment(apiParam.getParamName());
        paramDo.setFields(GsonUtil.objectToJsonString(FacadeParamFieldConverter.apiParamFieldList2DoList(apiParam.getFields())));
        paramDo.setPublicParam(EnumYn.N.getCode());
        paramDo.setCreateBy(SessionUtil.getSessionUserId());
        paramDo.setUpdateBy(SessionUtil.getSessionUserId());
        paramDo.setCreateDate(new Date());
        paramDo.setUpdateDate(new Date());
        return paramDo;
    }
    /**
     * apiParamList转doList
     *
     * @param apiParams
     * @return
     */
    public List<FacadeParamDo> apiParamList2DoList(List<ApiParam> apiParams){
        if(CollectionUtils.isEmpty(apiParams)){
            return null;
        }
        return Lists.newArrayList(Iterators.transform(apiParams.iterator(),FacadeParamConverter::apiParam2Do));
    }
}
